package rexsee.cartoon;

import rexsee.cartoon.component.Cartoon;
import rexsee.core.lang.RexseeLanguage;
import rexsee.core.style.RexseeDrawable;
import rexsee.core.transportation.PageSaver;
import rexsee.core.utilities.Utilities;
import rexsee.xml.XmlElement;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;

public class Frame {

	public final Action action;

	public String id = null;
	public String src = null;
	public Rect srcRect = null;
	public Rect rect = null;
	public Matrix matrix = null;
	public Path clip = null;
	public Paint clipBorder = null;
	public float[] reportScroll = null;
	public boolean reportPlay = false;

	public int frameWidth = 0;
	public int frameHeight = 0;

	public Bitmap bitmap = null;
	private final boolean isSupportClipPath = Cartoon.isSupportClipPath();

	public Frame(Action action, XmlElement element) {

		this.action = action;

		if (element.attributes == null) return;

		id = element.attributes.get(TagName.ATTR_ID);

		String s = element.attributes.get(TagName.ATTR_FRAME_SRC);
		if (s == null) return;
		s = s.trim();
		String check = s.toLowerCase();
		if (check.startsWith("#")) {
			src = s;
		} else if (check.startsWith("http://") || check.startsWith("https://") || check.startsWith("file://")) {
			action.actions.parser.browser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING + s.substring(s.lastIndexOf("/") + 1));
			src = PageSaver.download(action.actions.parser.browser, action.actions.parser.cacheDir, true, s).trim();
		} else {
			if (action.baseDir == null) return;
			while (s.startsWith("/")) {
				s = s.substring(1);
			}
			s = action.baseDir + "/" + s;
			action.actions.parser.browser.progressDialog.show(RexseeLanguage.PROGRESS_ONGOING + s.substring(s.lastIndexOf("/") + 1));
			src = PageSaver.download(action.actions.parser.browser, action.actions.parser.cacheDir, true, s).trim();
		}
		if (src == null) return;

		String size = element.attributes.get(TagName.ATTR_SIZE);
		if (size != null && size.contains("x")) {
			String[] c = size.trim().toLowerCase().split("x");
			frameWidth = Utilities.getInt(c[0], action.actionWidth);
			frameHeight = Utilities.getInt(c[1], action.actionHeight);
		} else {
			frameWidth = action.actionWidth;
			frameHeight = action.actionHeight;
		}

		srcRect = Actions.getRect(element.attributes.get(TagName.ATTR_FRAME_SRCRECT));
		rect = Actions.getRect(element.attributes.get(TagName.ATTR_FRAME_RECT));
		clip = Actions.getPath(element.attributes.get(TagName.ATTR_CLIP));
		clipBorder = Actions.getBorder(element.attributes.get(TagName.ATTR_CLIPBORDER));
		matrix = Actions.getMatrix(element.attributes.get(TagName.ATTR_MATRIX), frameWidth, frameHeight);

		reportPlay = "true".equalsIgnoreCase(element.attributes.get(TagName.ATTR_CARTOON_PLAY_REPORT));
		String reportStr = element.attributes.get(TagName.ATTR_CARTOON_SCROLL_REPORT);
		if (reportStr != null) {
			reportScroll = Utilities.getFloatArray(reportStr, 0);
		}

	}

	public void load() {

		if (bitmap != null && !bitmap.isRecycled()) return;

		if (frameWidth <= 0 || frameHeight <= 0 || src == null) {
			bitmap = Bitmap.createBitmap(10, 10, Config.ARGB_8888);
			Canvas canvas = new Canvas(bitmap);
			canvas.drawColor(Color.BLACK);
			return;
		}

		bitmap = Bitmap.createBitmap(frameWidth, frameHeight, Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		canvas.save();

		if (clip != null && isSupportClipPath) canvas.clipPath(clip);
		if (matrix != null) canvas.setMatrix(matrix);

		Rect targetRect = (rect != null) ? rect : new Rect(0, 0, frameWidth, frameHeight);

		if (src.startsWith("#")) {
			Drawable drawable = RexseeDrawable.getDrawable(action.actions.parser.browser, src, null);
			if (drawable != null) {
				drawable.setBounds((srcRect != null) ? srcRect : targetRect);
				drawable.draw(canvas);
			} else {
				canvas.drawColor(Color.BLACK);
			}
		} else {
			Bitmap srcBitmap = BitmapFactory.decodeFile(Uri.parse(src).getPath());
			if (srcBitmap != null) {
				canvas.drawBitmap(srcBitmap, srcRect, targetRect, null);
				srcBitmap.recycle();
			} else {
				canvas.drawColor(Color.BLACK);
			}
		}

		canvas.restore();
		if (clip != null && clipBorder != null) canvas.drawPath(clip, clipBorder);

		int size = bitmap.getWidth() * bitmap.getHeight() * 4;
		action.actions.bitmapSize += size;

	}

	public void destroy() {
		if (bitmap != null && !bitmap.isRecycled()) {
			int size = bitmap.getWidth() * bitmap.getHeight() * 4;
			action.actions.bitmapSize -= size;
			bitmap.recycle();
			bitmap = null;
		}
	}

}
